<?php
class RecordAddAction extends Action{
	
	public function main(){
		$this->_request['action'] = isset($this->_request['action']) ? $this->_request['action'] : 'default';
		switch ($this->_request['action']) {
			case 'modify':
				$this->__modify();
				break;
			default:
				$this->__load();
				break;
		}

	}

	private function __load(){
		$row = array();
		$ChannelIDarr = array(0);
		$StreamIDarr = array(0);
		if (isset($this->_request['TaskID'])) {
			$this->_response['TOPBARNAME'] = 'Modify Class';
			$TaskID = $this->_request['TaskID'];
			$RecordTaskDAO = new  RecordTaskDAO();
			$row =$RecordTaskDAO->getOneByID($TaskID);

			
			foreach ($row as $key => $value) {
				$this->_response[$key] = $value;
			}
			$ChannelIDarr = explode(",", $this->_response['ChannelID']);
			$StreamIDarr = explode(",", $this->_response['StreamID']);
			//debug($this->_response);
		}else{
			$this->_response['TOPBARNAME'] = 'Add New Record';
		}

		$selected = new Selected();
		$channelobj = new  ChannelInfoDAO();
		$channellist = $channelobj->getList();
		$channellist = $channellist ? $channellist : array();
		$streamobj = new StreamInfoDAO();
		$streamlist = $streamobj->getList();
		$streamlist = $streamlist ? $streamlist : array();
		$this->_response['channel_json'] = json_encode($channellist);
		//debug($StreamIDarr);
		$this->_response['defChannelID'] = $this->_request['defChannelID'];
		$this->_response['ClassID'] = $this->_request['ClassID'];
		$this->_response['ChannelID'] = $selected->getCustomize($channellist,"ChannelID","ChannelName",$ChannelIDarr,1);
		$this->_response['StreamID'] = $selected->getCustomize($streamlist,"StreamID","StreamName",$StreamIDarr,1);

		 

        //console.log(regdata);


		//debug($channellist);
		//debug ($this->_response);
		//$this->_response['default-row'] = $row;
	}


	private function __modify(){
		$inputarr =  array();
		$inputarr['TaskName'] = $this->_request['TaskName'];
		$inputarr['ChannelID'] = $this->_request['ChannelID'];
		$inputarr['StreamID'] = $this->_request['StreamID'];
		$inputarr['BeginTime'] = $this->_request['BeginTime'];
		$inputarr['EndTime'] = $this->_request['EndTime'];
		$inputarr['FilePath'] = $this->_request['FilePath'];
		$inputarr['FileName'] = $this->_request['FileName'];
		$TaskID = (int)$this->_request['TaskID']?(int)$this->_request['TaskID'] : 0;
		
		$RecordTaskDAO = new  RecordTaskDAO();
		if ($TaskID){
			$ret = $RecordTaskDAO->modify($TaskID,$inputarr);
			//echo ("modify successful");
		}else {
			$ret = $RecordTaskDAO->add($inputarr);
			//echo ("add sueecssful");
		}
		//die();
		//debug($inputarr);
		//die();
		//debug($ret);
		//$ClassName = $this->_request['ClassName'];
		//die();
		//debug($this->_request);
		$url = "index.php?do=Channel/channel_list&ClassID=".$this->_request['ClassID']."&list=record";
		//debug($url);
		//die();
		header("Location:$url");

		die();
		//debug($this->_request);
	}


	/*private __getClass($ChannelID){
		$obj = new ClassificationDAO();
		return $obj->

	}*/




}